﻿using UnityEngine;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour
{

    [SerializeField]
    private float platformSpeed = 1f;
    [SerializeField]
    private string playerTag = "Player";
    [SerializeField]
    public string menuTag = "Menu";
    [SerializeField]
    private string deathLineTag = "DeathLine";

    [SerializeField]
    PlatformBuilder platformPrefabs;


    GameObject _player;
    MenuController _menuController;

    void Awake()
    {
        if (Instance != null)
        {
            Destroy(gameObject);
            return;
        }
        Instance = this;

        DontDestroyOnLoad(gameObject);
    }

    void Update()
    {
        BuildPlatform();
    }

    public static GameManager Instance { get; private set; }


    public float PlatformSpeed { get => platformSpeed; }

    public string PlayerTag { get => playerTag; }

    public string DeathLineTag { get => deathLineTag; }

    public float PlatformDestroyOffsetY { get => 10f; }

    public GameObject PlayerGameObject 
    {
        get
        {
            if (_player == null)
            {
                _player = GameObject.FindGameObjectWithTag(playerTag);
            }
            return _player;
        }
    }

    public MenuController MenuController
    {
        get
        {
            if (_menuController == null)
            {
                _menuController = GameObject.FindGameObjectWithTag(menuTag).GetComponent<MenuController>();
            }
            return _menuController;
        }
    }


    public void BuildPlatform()
    {
        platformPrefabs.Build();
    }


    public void Death()
    {
        Debug.Log("gameobject death()");
        MenuController.ShowMenu();
    }

    public void ResetScene()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
        _player = null;
        _menuController = null;
    }
}
